package cn.acmenms.aspect;

import cn.acmenms.exception.TokenException;
import cn.acmenms.po.enums.ResponseCode;
import cn.acmenms.po.vo.AjaxResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @ClassName: ExceptionAspect
 * @Description: 全局异常处理切面
 * 利用 @ControllerAdvice + @ExceptionHandler 组合处理Controller层RuntimeException异常
 * @author: 神奇的鸭鸭
 * @Date: 2018/2/13 17:27
 */
@ControllerAdvice   // 控制器增强
@ResponseBody
public class ExceptionAspect {

    private Logger logger = LoggerFactory.getLogger(ExceptionAspect.class);

    /**
     * token异常
     */
    @ResponseStatus(HttpStatus.PAYMENT_REQUIRED)
    @ExceptionHandler(TokenException.class)
    public AjaxResponse handleTokenException(TokenException e) {
        logger.error("操作Token发生异常：", e);
        return new AjaxResponse().failure(ResponseCode.TOKENINVAILD, e.getMessage());
    }
}